package Depression;
/*
* Licensed Materials - Property of IBM
* 5724-X98 5724-Y15 5655-V82 
* Copyright IBM Corp. 1987, 2010. All Rights Reserved.
*
* Note to U.S. Government Users Restricted Rights: 
* Use, duplication or disclosure restricted by GSA ADP Schedule 
* Contract with IBM Corp.
*/



import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.NoSuchElementException;


/**
 * DateUtil
 */
public class DateUtil {
	
   public static Date now() {
   	  return Calendar.getInstance().getTime();
   }
   
   public static Date makeDate(int year, int month, int day) {
   	  Calendar cal = Calendar.getInstance();
   	  cal.set(Calendar.MILLISECOND,0);
 	  cal.set(Calendar.SECOND,0);
 	  cal.set(Calendar.MINUTE,0);
 	  cal.set(Calendar.HOUR_OF_DAY,0);
 	  cal.set(Calendar.YEAR,year);
 	  cal.set(Calendar.MONTH,month);
 	  cal.set(Calendar.DAY_OF_MONTH,day);
 	  return cal.getTime();
   }
	
   public static Date dateAsDay(Date date) {
   	  Calendar cal = Calendar.getInstance();
   	  cal.setTime(date);
   	  cal.set(Calendar.MILLISECOND,0);
   	  cal.set(Calendar.SECOND,0);
   	  cal.set(Calendar.MINUTE,0);
   	  cal.set(Calendar.HOUR_OF_DAY,0);
   	  return cal.getTime();
   }
   
   public static Date addDays(Date date, int days) {
   	  Calendar cal = Calendar.getInstance();
 	  cal.setTime(date);
 	  cal.add(Calendar.DATE,days);
 	  return cal.getTime();
   }
   
   public static int getDuration(Date startDate, Date endDate) {
   	  long startTime = startDate.getTime();
   	  long endTime = endDate.getTime();
   	  long oneHour = 3600 * 1000;
   	  // Add one hour here to handle the summer time shift
   	  return (int)((endTime - startTime + oneHour) / (24*oneHour));
   }
   
   public static int getAge(Date birthDate, Date nowDate) {
    
    Calendar birth = Calendar.getInstance();
    birth.setTime(birthDate);
    
    // Create a calendar object with today's date
    Calendar now = Calendar.getInstance();
    now.setTime(nowDate);
    
    // Get age based on year
    int age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
    
    // Add the tentative age to the date of birth to get this year's birthday
    birth.add(Calendar.YEAR, age);
    
    // If this year's birthday has not happened yet, subtract one from age
    if (now.before(birth)) {
        age--;
    }
    
    return age;   	
   }
   
   public static Iterator<Date> iterator(final Date startDate, final Date endDate) {
   	  return new Iterator<Date>() {
   	  	Date currentDate = startDate;
   	  	public boolean hasNext() {
   	  		if (currentDate.after(endDate))
   	  			return false;
   	  		return true;
   	  	}
   	  	public Date next() {
   	  	   	Date returnDate = currentDate;
   	  	   	if (hasNext()) {
   	  	   		currentDate = addDays(currentDate, 1);
   	  	   		return returnDate;
   	  	   	}
   	  	   	else
   	  	   	{
   	  	   		throw new NoSuchElementException(currentDate + " is after " + endDate); 
   	  	   	}
   	  	}
   	  	public void remove() {
   	  		throw new UnsupportedOperationException("Date iterator is read only") ;
   	  	}
   	  };
   }
   
   public static String format(Date date) {
   	  DateFormat formatter = DateFormat.getDateInstance();
   	  return formatter.format(date);
   }
}
